package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.view.View;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.SearchEstTypeVH;
import com.centaline.androidsalesblog.iml.RecyclerViewItemOnClickListener;
import com.centaline.androidsalesblog.model.businissobject.SearchEstTypeBo;
import com.centanet.framework.base.BaseAdapter1;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索房源数量
 * Created by guilin on 16/2/19.
 */
public class SearchEstTypeAdapter extends BaseAdapter1<SearchEstTypeVH> {

    private final ArrayList<SearchEstTypeBo> list;
    private RecyclerViewItemOnClickListener<SearchEstTypeBo> itemOnClickListener;
    private int selectPos;//选中位置

    public SearchEstTypeAdapter(Context context, ArrayList<SearchEstTypeBo> list) {
        super(context);
        this.list = list;
    }

    public void setItemOnClickListener(RecyclerViewItemOnClickListener<SearchEstTypeBo> itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public int getSelectPos() {
        return selectPos;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_search_est_type;
    }

    @Override
    protected SearchEstTypeVH viewHolder(View view) {
        return new SearchEstTypeVH(view);
    }

    @Override
    public void onBindViewHolder(final SearchEstTypeVH holder, int position) {
        holder.atv_est_type.setText(list.get(position).getTitle());
        final boolean isSelected = selectPos == position;
        holder.atv_est_type.setTextColor(isSelected ? 0xFFF13E06 : 0xFFFFFFFF);
        holder.atv_est_type.setBackgroundResource(isSelected ?
                R.drawable.search_est_type_sel_shape : R.drawable.search_est_type_nor_shape);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isSelected)
                    return;
                int temp = selectPos;
                selectPos = holder.getAdapterPosition();
                notifyItemChanged(temp);
                notifyItemChanged(selectPos);
                if (itemOnClickListener != null) {
                    itemOnClickListener.onItemOnClick(list.get(holder.getAdapterPosition()), 0);
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(SearchEstTypeVH holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
